package pe.com.bbva.modpre.persistencia.hibernate;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import pe.com.bbva.modpre.dominio.quartz.QrtzCronTriggers;
import pe.com.bbva.modpre.persistencia.CronTriggersDAO;
import pe.com.bbva.modpre.utileria.Busqueda;

@Repository
@SuppressWarnings("serial")
public class CronTriggersHibernate extends BaseHibernate<QrtzCronTriggers, Long> 
implements CronTriggersDAO {
	
	@Autowired
	public CronTriggersHibernate(SessionFactory sessionFactory){
		setSessionFactory(sessionFactory);
	}

	@Override
	public List<QrtzCronTriggers> buscarCronTriggers(
			QrtzCronTriggers qrtzCronTriggers) {
		Busqueda filtro = Busqueda.forClass(QrtzCronTriggers.class);
		if (qrtzCronTriggers != null) {
			if(qrtzCronTriggers.getTriggerGroup()!=null && StringUtils.isNotBlank(qrtzCronTriggers.getTriggerGroup())){
				System.out.println("+++ triggerGroup: " + qrtzCronTriggers.getTriggerGroup());
				filtro.add(Restrictions.ilike("triggerGroup",
							"%" + qrtzCronTriggers.getTriggerGroup() + "%"));
			}
			if(qrtzCronTriggers.getTriggerName()!=null && StringUtils.isNotBlank(qrtzCronTriggers.getTriggerName())){
				System.out.println("+++ triggerName: " + qrtzCronTriggers.getTriggerName());
				filtro.add(Restrictions.ilike("triggerName",
							"%" + qrtzCronTriggers.getTriggerName() + "%"));
			}
			if (qrtzCronTriggers.getCronExpression() != null && StringUtils.isNotBlank(qrtzCronTriggers.getCronExpression())) {
				filtro.add(Restrictions.ilike("cronExpression", "%" + qrtzCronTriggers.getCronExpression() + "%"));
			}
		}
		return buscar(filtro);
	}

}
